#include "methoddevelop.h"
#include "ui_methoddevelop.h"

MethodDevelop::MethodDevelop(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MethodDevelop)
{
    ui->setupUi(this);

    this->setWindowTitle("方法开发");
    //键盘设置
    ui->methodname->setFocus();
    keyboard->hide();
    isKeyBoardShow = 0;
    //抗锯齿
    chartView->setRenderHint(QPainter::Antialiasing);
    //隐藏点标识
    chart->legend()->hide();
    //初始图像占据一块区域
    ui->verticalLayout->addWidget(chartView);
    //ui美化
    this->initStyleSheet();
    //close keyboard
    connect(keyboard,&frmKeyBoard::hiding,this,&MethodDevelop::hidekeyboard);
}

MethodDevelop::~MethodDevelop()
{
    delete ui;
}

void MethodDevelop::initStyleSheet()
{
    QString qss;
    //先默认所有边框颜色为青色
    qss += "QLineEdit{border-width: 1px; border-radius: 10px; border-style: solid; border-color: rgb(98,183,152); }";
    //再将部分边框颜色改为珊瑚色
    qss += "#lightintensity{border-color:rgb(193,150,144);}";
    qss += "#displacement{border-color:rgb(193,150,144);}";
    qss += "#vector{border-color:rgb(193,150,144);}";
    /*
    qss += "QPushButton:hover{background-color:rgb(229,229,229); color:black;}";
    qss += "QPushButton:pressed,QPushButton:checked{background-color:rgb(0,118,215);}";
    qss += "#pushButton_closeKeyboard{background-color:rgba(0,0,0,0); border:0px}";
    qss += "#pushButton_closeKeyboard:hover{background-color:#b30220;}";
    qss += "#pushButton_space{min-width:500px;}";
    qss += "#pushButton_backspace,#pushButton_shift{min-width:100px;}";
    qss += "#pushButton_enter{min-width:120px;}";
    qss += "#pushButton_tab,#pushButton_ctrl{min-width:70px;}";
    qss += "#pushButton_capsLock{min-width:80px;}";
    qss += "#pushButton_up{min-width:150px;}";
    */
    qss += "QHeaderView::section {"
                   "background-color: black;"
                   "border: 1px solid gray;"
                   "height: 5px; /* 调整高度 */"
                   "}";
    this->setStyleSheet(qss);
}

void MethodDevelop::on_confirm_clicked()
{
    //生成图像，得到每一个点
    QString array = ui->vector->text();
    QStringList stringList = array.split(" ");
    QVector<double> data;
    for (const QString& str : stringList) {
        double number = str.toDouble();
        data.append(number);
    }
    QString array2 = ui->vector2->text();
    QStringList stringList2 = array2.split(" ");
    QVector<double> data2;
    for (const QString& str : stringList2) {
        double number = str.toDouble();
        data2.append(number);
    }
    //初始化
    chart->removeSeries(series);
    series->clear();
    //将点记录
    int size = 0;
    if(data.size() <= data2.size()){
        size = data.size();
    }else{
        size = data2.size();
    }
    for (int i = 0; i < size; ++i) {
        double x = data[i];
        double y = data2[i];
        series->append(x, y);
    }
    //将记录的点放在chart上
    chart->addSeries(series);
    chart->createDefaultAxes();
    ui->verticalLayout->addWidget(chartView);
}


void MethodDevelop::on_save_clicked()
{
    MethodManager methodManager;

    Method method;

    method.methodName = ui->methodname->text();
    qDebug() << ui->methodname->text();

    method.createdBy = Permission::User;
    qDebug() << Permission::User;

    method.lightIntensity = ui->lightintensity->text().toInt();
    qDebug() << ui->lightintensity->text().toInt();

    method.integrationTime = ui->integrationtime->text().toInt();
    qDebug() << ui->integrationtime->text().toInt();

    method.integrationCount = ui->integrationcount->text().toInt();
    qDebug() << ui->integrationcount->text().toInt();

    QString array = ui->displacement->text();
    QStringList stringList = array.split(" ");
    for (const QString& str : stringList) {
        int number = str.toInt();
        method.displacement.append(number);
    }
    qDebug() <<"位移中的内容:" << method.displacement;
    //消息框反馈
    bool success = methodManager.createMethod(method);
    if (success) {
        QMessageBox::warning(this,tr("create success"),tr("method saved success!"),QMessageBox::Ok);
        qDebug() << "Method created successfully";
    } else {
        QMessageBox::warning(this,tr("create failed"),tr("method saved failed!"),QMessageBox::Ok);
        qDebug() << "Failed to create method";
    }

    emit reset();
}


void MethodDevelop::on_keyboard_clicked()
{
    if(isKeyBoardShow == 0){
        keyboard->setGeometry(0, 175, 431, 166);
        keyboard->show();
        keyboard->initFocusWidget(ui->widget);
        isKeyBoardShow = 1;
    }else{
        keyboard->hide();
        isKeyBoardShow = 0;
    }
}


void MethodDevelop::on_home_clicked()
{
    keyboard->hide();
    isKeyBoardShow = 0;
    Permission::LastPage = 3;
    emit display(5); //l2
}


void MethodDevelop::on_selectmethod_clicked()
{
    keyboard->hide();
    isKeyBoardShow = 0;
    emit display(1); //s
}


void MethodDevelop::on_settings_clicked()
{
    keyboard->hide();
    isKeyBoardShow = 0;
    emit display(4); //set
}

//管理员相关管理
void MethodDevelop::hiding(bool s)
{
    if(s==0){
        ui->settings->hide();
    }else{
        ui->settings->show();
    }
}

//hide keyboard by keyboard's button
void MethodDevelop::hidekeyboard()
{
    keyboard->hide();
    isKeyBoardShow = 0;
}



